import { ref, reactive } from 'vue'
import { errorMsg, modalMsg } from '~/composables/utils'
import store from '~/store'
import { router } from '~/router'

// #region 修改密码
export function useRePassword(){
  const formDrawerRef = ref(null);

  // 表单
  const form = reactive({
    oldpassword: "",
    password: "",
    repassword: "",
  });

  // 表单验证规则
  const rules = {
    oldpassword: [
      {
        required: true,
        message: "旧密码不可为空",
        trigger: "blur",
      },
    ],
    password: [
      {
        required: true,
        message: "新密码不可为空",
        trigger: "blur",
      },
    ],
    repassword: [
      {
        required: true,
        message: "重复的新密码不可为空",
        trigger: "blur",
      },
    ],
  };

  // 提交
  const onSubmit = () => {
    if (form.password !== form.repassword) {
      errorMsg("两次输入的新密码不相同！");
      return;
    }
    formDrawerRef.value.close();
  };

  return {
    formDrawerRef, form, rules, onSubmit
  }
}
// #endregion

// #region 注销
export function useLogout(){
  const logout = () => {
    modalMsg("您确定要退出吗？").then(() => {
      store
        .dispatch("logout")
        .then(res => {
          router.push("/login");
        })
        .catch(err => errorMsg(err));
    });
  };

  return {
    logout
  }
}
// #endregion